package com.zyg.controller;

import com.alibaba.fastjson2.JSON;
import com.zyg.pojo.Grade;
import com.zyg.pojo.Grade;
import com.zyg.service.GradeService;
import com.zyg.service.GradeService;
import com.zyg.util.LayData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/grade")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @RequestMapping("/tolist")
    public  String tolist(){
        return "grade/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public  String list(Grade grade , @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        // 查询数据
        LayData layData = new LayData();
        int totalCount = gradeService.count(grade);
        layData.setCount(totalCount);

        layData.setData(gradeService.findAll(grade, (page - 1) * limit, limit));

        layData.setCode(0);

        layData.setMsg("查询成功");
        // 数据返回
        String data = JSON.toJSONString(layData);

        return data;
    }

    @RequestMapping("/toadd")
    public String toAdd(){
        return "grade/add";
    }

    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(Grade grade){
        int result = gradeService.addGrade(grade);
        if (result > 0) {
            return JSON.toJSONString(new LayData(0, "添加成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "添加失败"));
        }
    }

    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") int id, Model model){
        Grade grade = gradeService.findGradeById(id);
        model.addAttribute("grade", grade);
        return "grade/update";
    }

    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateSave(Grade grade){
        int result = gradeService.updateGrade(grade);
        if (result > 0) {
            return JSON.toJSONString(new LayData(0, "更新成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "更新失败"));
        }
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String deleteGrade(@PathVariable("id") int id){
        int result = gradeService.deleteGrade(id);
        if (result > 0) {
            return JSON.toJSONString(new LayData(0, "删除成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "删除失败"));
        }
    }

}
